package de.ihk.beans;

import java.io.Serializable;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.event.ActionEvent;


import de.ihk.data.Admin;
import de.ihk.datamanagment.MysqlConnector;

@ManagedBean
@ViewScoped
public class AdminBean implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private List<Admin> admins;
	private Admin[] selectedAdmins;

	private int id = 1;
	private String newVorname;
	private String newNachname;
	private String newEMail;
	private String newPassword;
	private int superadmin = 0;

	private ResultSet rs = null;
	private MysqlConnector conn = null;

	public AdminBean() throws ClassNotFoundException, SQLException {
		admins = new ArrayList<Admin>();
		System.out.println("Admins werden erstellt");
		conn = new MysqlConnector();
		updateList();

	}

	public void submit(ActionEvent actionEvent) throws SQLException {
		System.out.println(newEMail);
		rs = conn.getAdmins();
		int maxid = 1;
		while (rs.next()) {
			if (rs.getInt(1) > maxid) {
				maxid = rs.getInt(1);
			}
		}
		
		conn.addAdminToDb(maxid+1, newNachname, newVorname, newEMail, newPassword, superadmin);
		updateList();
	}

	public void updateList() throws SQLException {
		rs = conn.getAdmins();
		admins.removeAll(admins);
		while (rs.next()) {
			admins.add(new Admin(rs.getInt(1), rs.getString(2), rs.getString(3), rs.getString(4), rs.getString(5), rs.getInt((6))));
		}

	}

	public void deleteSelected(ActionEvent actionEvent) throws SQLException {
		for (Admin i : selectedAdmins) {
			conn.mysqlUpdateQuery("DELETE FROM admins WHERE id = " + i.getId() + ";");

		}
		updateList();
	}

	public void deleteAll(ActionEvent actionEvent) throws SQLException {
		conn.mysqlUpdateQuery("TRUNCATE admins");
		updateList();
	}

	public List<Admin> getAdmins() {
		return admins;
	}

	public void setAdmins(List<Admin> admins) {
		this.admins = admins;
	}

	public Admin[] getSelectedAdmins() {
		return selectedAdmins;
	}

	public void setSelectedAdmins(Admin[] selectedAdmins) {
		this.selectedAdmins = selectedAdmins;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getNewVorname() {
		return newVorname;
	}

	public void setNewVorname(String newVorname) {
		this.newVorname = newVorname;
	}

	public String getNewNachname() {
		return newNachname;
	}

	public void setNewNachname(String newNachname) {
		this.newNachname = newNachname;
	}

	public String getNewEMail() {
		return newEMail;
	}

	public void setNewEMail(String newEMail) {
		this.newEMail = newEMail;
	}

	public String getNewPassword() {
		return newPassword;
	}

	public void setNewPassword(String newPassword) {
		this.newPassword = newPassword;
	}

	public int getSuperadmin() {
		return superadmin;
	}

	public void setSuperadmin(int superadmin) {
		this.superadmin = superadmin;
	}

}
